﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sprite_Prop : Interactable
{
    public static float Rotate_speed = 2.0f;
    public static float Scale = 1.2f;
    public Objectinfo this_prop;
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        this.transform.Rotate(new Vector3(0, Rotate_speed, 0), Space.World);
    }

    public override void Activate()
    {
        base.Activate();
        Inventory._instance.Add_Concrete_Item(this_prop);

        Destroy(gameObject);
    }

    public void Set_Obj_Info(int id)
    {
        Objectinfo Objectinfo = ObjectsInfo._instance.getObjectinfobyid(id);
        Set_Obj_Info(Objectinfo);
    }

    public void Set_Obj_Info(Objectinfo prop_info)
    {
        SpriteRenderer sprite_render = this.GetComponent<SpriteRenderer>();
        this_prop = prop_info;

        this.transform.localScale = new Vector3(Scale, Scale, Scale);
        sprite_render.sprite = SpriteManager.getInstance().LoadAtlasSprite(this_prop.atlas_path, this_prop.icon_name);
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Interactable"))
        {
            BoxCollider this_box = this.GetComponent<BoxCollider>();
            Vector3 this_box_position = this_box.bounds.center;

            Debug.Log("Enter");
            Collider collider = collision.collider;
            Vector3 others_position = collider.bounds.center;

            Debug.Log("other_center:" + others_position);
            Debug.Log("this_center:" + this_box_position);
            Vector3 delta = new Vector3(this_box_position.x - others_position.x, 0, this_box_position.z - others_position.z);
            delta = delta.normalized * 0.1f;

            this.transform.Translate(delta);
        }
    }
}
